package pers.mine.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import pers.mine.po.Admin;
import pers.mine.service.inter.AdminService;
import pers.mine.vo.Info;

/**
 * 管理员Controller
 * 
 * @author Mine
 * @since 2016年10月18日_上午11:43:20
 */
@Controller
@RequestMapping("/admin")
public class AdminController {
	private Info logininfo = new Info();// 放到登录页的信息

	@Autowired
	AdminService adminService;

	/**
	 * 获得登录页面
	 */
	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public String adminLogin() {
		return "adminLogin";
	}

	/**
	 * 获得welcome页面
	 */
	@RequestMapping(value = "/welcome", method = RequestMethod.GET)
	public String getWelcome() {
		return "admin/welcome";
	}

	/**
	 * 获得adminIndex页面
	 */
	@RequestMapping(value = "/index", method = RequestMethod.GET)
	public String getAdminIndex(HttpSession session, Model model) {
		return "admin/adminIndex";
	}

	/**
	 * 获得logout页面
	 */
	@RequestMapping(value = "/logout", method = RequestMethod.GET)
	public String getAdminLogout(HttpSession session) {
		return "adminLogout";
	}

	/**
	 * 执行登录操作
	 * 
	 * @param id
	 * @param name
	 * @return
	 */
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public String adminLogin(String id, String pw, Model model,
			HttpSession session) {
		Admin one = adminService.login(id, pw);
		System.out.println(one);
		if (one != null) {
			// 放入session
			session.setAttribute("admin", one);
			// 内部转发
			return "redirect:index";
		} else {
			logininfo.setType(Info.ERROR);
			logininfo.setMsg("用户名或密码输入有误");
			model.addAttribute("logininfo", logininfo);
			return "adminLogin";
		}

	}
}
